package com.kin207.netty.message.mqtt.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

import io.netty.handler.codec.http.HttpResponse;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kin207
 * @date 2020-07-27 14:27
 * @desc
 */
public class HttpClientUtil {
    public static JSONObject doGet(String url, Map<String, String> param) {

        // 鍒涘缓Httpclient瀵硅薄
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 鍒涘缓uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 鍒涘缓http GET璇锋眰
            HttpGet httpGet = new HttpGet(uri);

            // 鎵ц璇锋眰
            response = httpclient.execute(httpGet);
            // 鍒ゆ柇杩斿洖鐘舵�佹槸鍚︿负200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return JSONObject.parseObject(resultString);
    }

    public static JSONObject doGet(String url) {
        System.out.println(url);
        return doGet(url, null);
    }

    public static JSONObject doPost(String url, JSONObject param) {
        // 鍒涘缓Httpclient瀵硅薄
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 鍒涘缓Http Post璇锋眰
            HttpPost httpPost = new HttpPost(url);
	         StringEntity se = new StringEntity(param.toString(),"utf-8");
	         se.setContentType("text/json");
	         se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
	         httpPost.setEntity(se);
	         httpPost.setEntity(se);
	         response = httpClient.execute(httpPost);
	         resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSONObject.parseObject(resultString);
    }

    public static JSONObject doPost(String url) {
        return doPost(url, null);
    }

    public static JSONObject doPostJson(String url, JSONObject param,String token) {
        // 鍒涘缓Httpclient瀵硅薄
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 鍒涘缓Http Post璇锋眰
            HttpPost httpPost = new HttpPost(url);
            if(StringUtils.isNotEmpty(token)) {
            httpPost.addHeader("X-SDX-Token", token);
            }
	         StringEntity se = new StringEntity(param.toString(),"utf-8");
	         se.setContentType("text/json");
	         se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
	         httpPost.setEntity(se);
	         httpPost.setEntity(se);
	         response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSONObject.parseObject(resultString);
    }
    public static JSONObject doPostLoginJson(String url, String json,String token) {
        // 鍒涘缓Httpclient瀵硅薄
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 鍒涘缓Http Post璇锋眰
            HttpPost httpPost = new HttpPost(url);
            if(StringUtils.isNotEmpty(token)) {
            httpPost.addHeader("X-SDX-Token", token);
            }
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            // 鍒涘缓璇锋眰鍐呭
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 鎵цhttp璇锋眰
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSONObject.parseObject(resultString);
    }
}
